package com.cyc.controller;

import com.alibaba.fastjson.JSON;
import com.cyc.entity.Product;
import com.cyc.service.CommentService;
import com.cyc.service.OrderDetailService;
import com.cyc.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    CommentService commentService;

    @RequestMapping("/all")
    @ResponseBody
    public String productall(Integer pageNum ,Integer pageSize,HttpSession session){
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=16;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = productService.selectAll();
        PageInfo pageInfo=new PageInfo<>(list);
        session.setAttribute("indexPageInfo",pageInfo);
        return JSON.toJSONString(list);
    }

    @RequestMapping("/select")
    public String selectProduct(Integer cateId,Integer pageNum,Integer pageSize,Model model){
        if (pageNum==null){
            pageNum=1;
        }else if (pageNum<1){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = productService.selectByCategoryId(cateId);
        PageInfo pageInfo=new PageInfo<>(list);
        model.addAttribute("productList",list);
        model.addAttribute("pageInfo",pageInfo);
        return "product-list";
    }

    @RequestMapping("selectbyid")
    public String selectById(Integer id, Model model){
        Product product = productService.selectByPrimaryKey(id);
        model.addAttribute("product",product);
        return "product-view";
    }

    @RequestMapping("manall")
    @ResponseBody
    public String manall(Integer page,Integer limit){
        Map<String, Object> map=new HashMap<>();
        PageHelper.startPage(page,limit);
        List<Product> list = productService.selectAll();
        PageInfo pageInfo=new PageInfo(list);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",pageInfo.getTotal());
        map.put("data",list);
        return JSON.toJSONString(map);
    }


    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    @RequestMapping("del")
    public String del(Integer id){
        Map<String, Object> map=new HashMap<>();
        commentService.deleteByProductId(id);
        orderDetailService.deleteByProductId(id);
        int i = productService.deleteByPrimaryKey(id);
        if (i>0){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return JSON.toJSONString(map);
    }


    @ResponseBody
    @RequestMapping(value = "add")
    public String add(String name,String description,Integer categorylevel1id,Float price,Integer stock,@RequestParam CommonsMultipartFile filename){
        Map<String, Object> map=new HashMap<>();
        Product product=new Product();
        product.setName(name);
        product.setDescription(description);
        product.setCategorylevel1id(categorylevel1id);
        product.setPrice(price);
        product.setStock(stock);
        String uploadFileName= filename.getOriginalFilename();
        product.setFilename(uploadFileName);
        String filePath="C:\\Users\\cyc\\Desktop\\easybuyssm\\web\\images\\product";
        try{
            filename.transferTo(new File(filePath,uploadFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = productService.insert(product);
        if (i>0){
            map.put("code",1);
            map.put("msg","添加成功");
        }else {
            map.put("code",0);
            map.put("msg","添加失败");
        }

        return JSON.toJSONString(map);
    }

    @ResponseBody
    @RequestMapping(value = "update")
    public String update(Integer id,String name,String description,Integer categorylevel1id,Float price,Integer stock,@RequestParam CommonsMultipartFile filename){
        Map<String, Object> map=new HashMap<>();
        Product product=new Product();
        product.setId(id);
        product.setName(name);
        product.setDescription(description);
        product.setCategorylevel1id(categorylevel1id);
        product.setPrice(price);
        product.setStock(stock);
        String uploadFileName= filename.getOriginalFilename();
        product.setFilename(uploadFileName);
        String filePath="C:\\Users\\cyc\\Desktop\\easybuyssm\\web\\images\\product";
        try{
            filename.transferTo(new File(filePath,uploadFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = productService.updateByPrimaryKey(product);
        if (i>0){
            map.put("code",1);
            map.put("msg","修改成功");
        }else {
            map.put("code",1);
            map.put("msg","修改失败");
        }

        return JSON.toJSONString(map);
    }

}
